/**
 * 
 */
package kr.co.insoft.core.service.configration.cdt.impl;

import java.util.List;

import kr.co.insoft.auth.model.security.User;
import kr.co.insoft.core.model.configration.cdt.CDTEntityModel;
import kr.co.insoft.core.model.configration.cdt.CDTListParam;
import kr.co.insoft.core.model.paging.PagingModel;
import kr.co.insoft.core.repository.configration.cdt.CDTMetadataRepository;
import kr.co.insoft.core.repository.configration.cdt.CDTRepository;
import kr.co.insoft.core.service.configration.cdt.CDTService;
import kr.co.insoft.framework.exceptions.ExceptionCode;
import kr.co.insoft.framework.exceptions.RepositoryException;
import kr.co.insoft.framework.exceptions.ServiceException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author IN-SOFT Inc. GoodwillDD(sylee@in-soft.co.kr)
 * 
 */
@Service
public class CDTServiceImpl implements CDTService {

	private static final Logger logger = LoggerFactory
			.getLogger(CDTServiceImpl.class);
	
	@Autowired
	CDTRepository cdtRepository;

	@Autowired
	CDTMetadataRepository cdtMetadataRepository;

	@Override
	public PagingModel getLists(PagingModel pagingModel)
			throws ServiceException {
		try {
			Authentication auths = SecurityContextHolder.getContext()
					.getAuthentication();
			User user = (User) auths.getPrincipal();
			pagingModel.setSearchOrCondition(user.getQueryAuthParam());
			List<CDTEntityModel> lists = cdtRepository.getLists(pagingModel);
			int totalCount = cdtRepository.getTotalCount(pagingModel);
			return new PagingModel(lists, totalCount, pagingModel);
		} catch (RepositoryException e) {
			logger.error(ExceptionCode.SERVICE_EXCEPTION, e);
			throw new ServiceException(ExceptionCode.SERVICE_EXCEPTION, e);
		}
	}
	
	@Override
	public List<CDTEntityModel> getCDTs() throws ServiceException {
		try {
			Authentication auths = SecurityContextHolder.getContext()
					.getAuthentication();
			CDTListParam param = new CDTListParam((User) auths.getPrincipal());
			return cdtRepository.getCDTs(param);
		} catch (RepositoryException e) {
			logger.error(ExceptionCode.SERVICE_EXCEPTION, e);
			throw new ServiceException(ExceptionCode.SERVICE_EXCEPTION, e);
		}
	}

	@Override
	@Transactional(rollbackFor = ServiceException.class)
	public void createCDT(CDTEntityModel cdtEntityModel)
			throws ServiceException {
		try {
			Authentication auths = SecurityContextHolder.getContext()
					.getAuthentication();
			User user = (User) auths.getPrincipal();
			cdtEntityModel.setCompanyid(user.getCompanyId());
			cdtEntityModel.setUserid(user.getId());
			cdtRepository.createCDT(cdtEntityModel);
			cdtMetadataRepository.createCDTMetadata(cdtEntityModel);
		} catch (RepositoryException e) {
			logger.error(ExceptionCode.SERVICE_EXCEPTION, e);
			throw new ServiceException(ExceptionCode.SERVICE_EXCEPTION, e);
		}
	}

	@Override
	public CDTEntityModel getCDT(String userTypeId) throws ServiceException {
		try {
			Authentication auths = SecurityContextHolder.getContext()
					.getAuthentication();
			CDTEntityModel cdtEntityModel = new CDTEntityModel();
			cdtEntityModel.setUserTypeId(userTypeId);
			User user = (User) auths.getPrincipal();
			cdtEntityModel.setCompanyid(user.getCompanyId());
			cdtEntityModel.setUserid(user.getId());
			return cdtMetadataRepository.getCDTMetadata(cdtRepository
					.getCDT(cdtEntityModel));
		} catch (RepositoryException e) {
			logger.error(ExceptionCode.SERVICE_EXCEPTION, e);
			throw new ServiceException(ExceptionCode.SERVICE_EXCEPTION, e);
		}
	}

	@Override
	@Transactional(rollbackFor = ServiceException.class)
	public void editCDT(CDTEntityModel cdtEntityModel) throws ServiceException {
		try {
			Authentication auths = SecurityContextHolder.getContext()
					.getAuthentication();
			User user = (User) auths.getPrincipal();
			cdtEntityModel.setCompanyid(user.getCompanyId());
			cdtEntityModel.setUserid(user.getId());
			cdtRepository.editCDT(cdtEntityModel);
			cdtMetadataRepository.removeCDTMetadata(cdtEntityModel);
			cdtMetadataRepository.createCDTMetadata(cdtEntityModel);
		} catch (RepositoryException e) {
			logger.error(ExceptionCode.SERVICE_EXCEPTION, e);
			throw new ServiceException(ExceptionCode.SERVICE_EXCEPTION, e);
		}
	}

	@Override
	@Transactional(rollbackFor = ServiceException.class)
	public void removeCDT(CDTEntityModel cdtEntityModel)
			throws ServiceException {
		try {
			Authentication auths = SecurityContextHolder.getContext()
					.getAuthentication();
			User user = (User) auths.getPrincipal();
			cdtEntityModel.setCompanyid(user.getCompanyId());
			cdtEntityModel.setUserid(user.getId());
			cdtRepository.removeCDT(cdtEntityModel);
		} catch (RepositoryException e) {
			logger.error(ExceptionCode.SERVICE_EXCEPTION, e);
			throw new ServiceException(ExceptionCode.SERVICE_EXCEPTION, e);
		}
	}
}
